<?php

/**
 * 站内消息
 * File Name：NewsController.php
 * File Encoding：UTF-8
 * File New Time：2014-8-19 10:30:36
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class NewsController extends UserLoginBaseC {

    //短信列表
    public function actionList() {
        $msg = MembersMsgModel::model();
        $msgText = MembersMsgTextModel::model();
        $where = array('userid' => Yii::app()->user->id, 'answerid' => 0);
        $count = $msg->count($msg->where($where));
         //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        $page = self::page($count, 20,$pageId);
        $criteria = $msg->where($where);
        $criteria->order = 'datetime desc,id desc';
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $data = $msg->findAll($criteria);
        //私信ID
        $ids = array();
        foreach ($data as $k => $r) {
            $r = $r->attributes;
            //查找上一次对话
            $find = $msg->find($msg->where(array('userid' => Yii::app()->user->id, 'answerid' => $r['id'], 'order' => 'datetime desc,id desc')));
            if ($find) {
                $find = $find->attributes;
                $ids[] = $find['id'];
                unset($find['id']);
                $msgid = $find['msgid'];
            } else {
                $find = array();
                $ids[] = $r['id'];
            }
            if (!$msgid) {
                $msgid = $r['msgid'];
            }
            $text = $msgText->findByPk($msgid);
            if (empty($text)) {
                $text = array();
            }
            $data[$k] = array_merge($r, $find, $text->attributes);
            $data[$k]['answerid'] = MembersModel::model()->getUserInfo((int) $r['suid']);
        }
        //设置已读
        $msg->setRead(Yii::app()->user->id, $ids);
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
        $this->assign('data', $data);
        $this->render();
    }

    //私信详情
    public function actionMsginfo() {
        $msg = MembersMsgModel::model();
        $msgText = MembersMsgTextModel::model();
        $id = (int) Yii::app()->request->getParam('id');
        if (empty($id)) {
            $this->error('参数不能为空！');
        }
        $where = array('userid' => Yii::app()->user->id, 'id' => $id, 'answerid' => 0);
        $info = $msg->find($msg->where($where));
        if (empty($info)) {
            $this->error('该私信对话不存在！');
        }
        $list = $msg->findAll($msg->where(array('userid' => Yii::app()->user->id, 'answerid' => $info->id, 'order' => 'datetime asc,id asc')));
        if (empty($list)) {
            $list = array();
        }
        array_unshift($list, $info);
        //私信ID
        $ids = array();
        foreach ($list as $k => $r) {
            $text = $msgText->findByPk($r['msgid']);
            if (empty($text)) {
                $text = array();
            }
            $ids[] = $r['id'];
            $list[$k] = array_merge($list[$k]->attributes, $text->attributes);
        }
        //查询出与我回话的另一用户
        $answerid = $info['suid'];
        $answerInfo = MembersModel::model()->getUserInfo((int) $answerid);
        //设置已读
        $msg->setRead(Yii::app()->user->id, $ids);

        $this->assign('list', $list);
        $this->assign('info', $info);
        $this->assign('answerid', $answerid);
        $this->assign('answerInfo', $answerInfo);
        $this->render();
    }

    //发送短信
    public function actionSend() {
        $post = $_POST;
        //私信发送给谁
        $uid = (int) $_POST['uid'];
        if ($uid == Yii::app()->user->id) {
            $this->error('您不能给自己发私信！');
        }
        //私信内容
        $note = trim($_POST['note']);
        if (empty($note)) {
            $this->error('请先写点什么吧！');
        }
        //组装提交数据
        $post['recuid'] = $uid;
        $post['note'] = $note;
        unset($post['uid']);
        //提交
        $mid = MembersMsgModel::model()->msgAdd(Yii::app()->user->id, $post);
        if ($mid) {
            $this->success('发送成功！');
        } else {
            $this->error('发送失败！');
        }
    }

}
